﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LimpaGaragem.DAO.Generic
{
    public class City
    {
        /// <summary>
        /// Return list of city
        /// </summary>
        /// <returns></returns>
        public List<LimpaGaragem.Model.Generic.City> GetListOfCity(int IDState)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();
                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_STATE", IDState));

                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR019_ListCities", 0, objListOfParameters);

                List<LimpaGaragem.Model.Generic.City> objListOfCity = new List<LimpaGaragem.Model.Generic.City>();
                foreach (DataRow objDataRow in objDataTable.Rows)
                {
                    LimpaGaragem.Model.Generic.City objCity = new LimpaGaragem.Model.Generic.City();
                    objCity.IDState = IDState;
                    objCity.ID = Convert.ToInt32(objDataRow["ID_CITY"]);
                    objCity.Name = objDataRow["NM_CITY"].ToString();
                    objListOfCity.Add(objCity);
                }
                return objListOfCity;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Return data of city
        /// </summary>
        /// <returns></returns>
        public LimpaGaragem.Model.Generic.City GetCity(int IDCity)
        {
            try
            {
                Framework.DataAccess objDataAccess = new Framework.DataAccess();

                //PARAMETERS
                List<SqlParameter> objListOfParameters = new List<SqlParameter>();
                objListOfParameters.Add(new SqlParameter("@ID_CITY", IDCity));

                //RETURN DATA
                DataTable objDataTable = objDataAccess.ReturnDataTable(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, Framework.DataAccess.QueryType.Procedure, "PR019_SelectCity", 0, objListOfParameters);

                LimpaGaragem.Model.Generic.City objCity = null;
                if (objDataTable.Rows.Count > 0)
                {
                    objCity = new LimpaGaragem.Model.Generic.City();
                    objCity.ID = IDCity;
                    objCity.IDState = Convert.ToInt32(objDataTable.Rows[0]["ID_STATE"]);
                    objCity.Name = objDataTable.Rows[0]["NM_CITY"].ToString();
                }
                return objCity;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
